/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.mapper.AdminMapper;
import cn.webrx.wxs.mapper.MenuMapper;
import cn.webrx.wxs.mapper.OrderMapper;
import cn.webrx.wxs.utils.Md5SaltUtil;
import cn.webrx.wxs.vo.VoLogin;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: wxshop - AdminController
 * <p>Powered by webrx On 2021-04-16 19:25:49
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller @RequestMapping("/admin")
public class AdminController {
    @Autowired
    private MenuMapper menudao;
    @Autowired
    private OrderMapper orderdao;

    @Autowired
    private AdminMapper admindao;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("/admin/index");
        mv.addObject("menu", menudao.queryMenu());
        return mv;
    }

    @GetMapping("/login")
    public void login() {
    }

    /**
     * 后台工作台基本数据处理
     *
     * @return
     */
    @GetMapping("/stat")
    public String stat(Model m) {
        //更新时间
        m.addAttribute("time", new Date());
        //今日成交订单
        m.addAttribute("todayorder", orderdao.order());
        return "admin/stat";
    }

    @GetMapping("/modifypassword")
    public void modifypassword() {
    }


    @PostMapping("/checklogin")
    @ResponseBody
    public Map<String, Object> checkLogin(HttpSession session, VoLogin vl) {
        Map<String, Object> map = new HashMap<String, Object>();
        //检查验证码是否正确
        if(session.getAttribute("captcha").equals(vl.getCode())){
            var ma = admindao.checkLogin(vl.getAccount());
            if(ma == null){
                map.put("msg", "登录失败，请检查账号密码。");
                map.put("code", 0);
            }else if(Md5SaltUtil.verfiyPassword(vl.getAccount(), vl.getPassword(), ma.get("salt"), ma.get("password"))){
                map.put("msg", "登录成功，正在进入系统。");
                map.put("code", 1);
                session.setAttribute("admin", vl.getAccount());
                session.setAttribute("adminname", vl.getAccount());
                session.setAttribute("rolename", "超级管理员");
            }else{
                map.put("msg", "登录失败，请检查账号密码。");
                map.put("code", 0);
            }
        }else{
            map.put("msg", "登录失败，验证码不正确。");
            map.put("code", 0);
        }
        return map;
    }

    @GetMapping("/captcha")
    public void captcha(HttpServletRequest req, HttpServletResponse res) throws Exception {
        // 使用gif验证码
        GifCaptcha gifCaptcha = new GifCaptcha(130, 48, 4);
        CaptchaUtil.out(gifCaptcha, req, res);
    }

    @GetMapping("/logout")
    public String logout(HttpSession s) {
        s.removeAttribute("admin");
        return "redirect:/";
    }
}
